<?php
namespace app\api\controller;

use app\BaseController;
use app\model\Project as SelfModel;
use app\model\LeasingCompany;

use think\facade\Cookie;
use think\Response;
use think\Request;
class Project extends Base
{


    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\Project";
        $this->list_rows = 10;
        $this->index = ['id','title','rent_price','name','mobile','city','remark','create_time'];
        $this->all =  ['id','title','rent_price','name','mobile','city','remark','create_time'];
        $this->my = ['id','title','rent_price','name','mobile','city','remark','create_time'];
        $this->view = ['id','title','rent_price','name','mobile','city','remark','create_time'];
        $this->msg = '项目';
        
        
    }

    public function index(Request $request)
    {

        try {
            $data = $this->model::withSearch(array_keys($request->data), $request->data)
                ->field($this->index)
//                ->withoutField($this->without_field)
//                ->where($this->status)
                ->where($this->where)
                ->order($this->order)
                ->paginate($this->list_rows)->each(function($item,$key){
                  $item['device_count'] = \app\model\Device::where('project_id',$item['id'])->count();;
                });
            if($this->append){
                $data->append($this->append);
            }
            if($this->hidden){
                $data->hidden($this->hidden);
            }
            return ['code' => 200, 'msg' => $this->msg . '列表', 'data' => $data,'all_device_count'=> \app\model\Device::where('company_id',$request->company_id)->count()];
        } catch (\Exception $e) {
            return ['code'=>$e->getCode(),'msg'=>$e->getMessage(),'line'=>$e->getTrace()];
        }
    }

    public function all(Request $request)
    {

        try {
            $data = $this->model::withSearch(array_keys($request->data), $request->data)
                ->field($this->index)
//                ->withoutField($this->without_field)

                ->where($this->where)
                ->order($this->order)
                ->select();
            foreach ($data as &$v){
                $v['device_count'] = \app\model\Device::where('project_id',$request->project_id)->count();
            }
            if($this->append){
                $data->append($this->append);
            }
            if($this->hidden){
                $data->hidden($this->hidden);
            }
            return ['code' => 200, 'msg' => $this->msg . '列表', 'data' => $data,'all_device_count'=> \app\model\Device::where('company_id',$request->company_id)->count()];
        } catch (\Exception $e) {
            return ['code'=>$e->getCode(),'msg'=>$e->getMessage(),'line'=>$e->getTrace()];
        }
    }

    public function view(Request $request)
    {
        try {

            $data = $this->model::field($this->view)->find($request->id);


            return ['code' => 200, 'msg' => $this->msg . '详情', 'data' => $data];
        } catch (\Exception $e) {
            return ['code' => $e->getCode(), 'msg' => $e->getMessage(),];
        }
    }




}
